//Stall Reservations:https://www.luogu.com.cn/problem/P2859


#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

const int N = 5e4 + 10;

struct node
{
	int l;
	int r;
	int c;
	int i;
	
	int operator<(const node& n)const 
	{
		return l < n.l;
	}
}a[N];


struct P
{
	int r;
	int c;

};

struct cmp
{
	int operator ()(const P& n1,const P& n2)const
	{
		return n1.r > n2.r;
	}	
};


bool cmp1(const node& n1,const node& n2)
{
	return n1.i < n2.i;
}

int n;


int main()
{
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		scanf("%d%d",&a[i].l,&a[i].r);
		a[i].i = i;
	}
	
	sort(a,a+n);
	int ret = 0;
	priority_queue<P,vector<P>,cmp> q;
	for(int i = 0; i < n; i++)
	{
		int left = a[i].l;
		int right = a[i].r;
		if(q.size() == 0 || q.top().r >= left)
		{
			ret++;
			a[i].c = ret;
			q.push({right,ret});
		}
		else
		{
			P c = q.top();
			q.pop();
			a[i].c = c.c;
			q.push({right,c.c});
		}
	}
	sort(a,a+n,cmp1);
	printf("%d\n",ret);
	for(int i = 0; i < n; i++)
	{
		printf("%d\n",a[i].c);
	}
	
	return 0;
}

